package com.esri.viewer
{
	import com.esri.viewer.*;
	
	import comValue.*;
	
	import flash.net.URLVariables;
	
	import mx.managers.CursorManager;
	import mx.rpc.AsyncResponder;
	import mx.rpc.Responder;
	import mx.rpc.events.*;
	import mx.rpc.http.HTTPService;
	
	public class HttpServices implements IHttpServices
	{
		private var pHttpServices:HTTPService;
		private var infoKey:String;
		public function HttpServices(s_Url:String,s_infoKey:String)
		{
			infoKey=s_infoKey;
			initHttpservices(s_Url);
		}
		//给相关httpservices赋值，并执行相关查询结果
		public  function set setUrlVariable(value:URLVariables):void
		{
			CursorManager.setBusyCursor();
			value.r=Math.random();
			pHttpServices.request=value;
			pHttpServices.send();
		}
		//初始化httpservices查询类
		private function initHttpservices(s_Url:String):void
		{
			pHttpServices=new HTTPService();
			pHttpServices.url=s_Url;
			pHttpServices.resultFormat="e4x";
			pHttpServices.requestTimeout=600;
			//监听事件
			pHttpServices.addEventListener(ResultEvent.RESULT,pHTTPService_resultHandler);
			pHttpServices.addEventListener(FaultEvent.FAULT,pHTTPService_faultHandler);
	     }
		//执行httpservices查询成功时执行相关操作
		protected function pHTTPService_resultHandler(event:ResultEvent):void
		{
			var pXML:XML=XML(event.result);
			if(!pXML) return;
			var pXmlList:XMLList=pXML.node;
			var infoData:Object={
				key:infoKey,
				value:pXmlList	
			}	
			ViewerContainer.dispatchEvent(new AppEvent(AppEvent.DATA_HTTPSERVICES,infoData));		
		}
		//执行httpservices查询失败时提供相关提示信息
		protected function pHTTPService_faultHandler(event:FaultEvent):void
		{
			CursorManager.removeAllCursors();
			var sInfo:String="Error: ";
			sInfo+="Event Target: " + event.target + "\n\n";
			sInfo+="Event Type: " + event.type + "\n\n";
			sInfo+="Fault Code: " + event.fault.faultCode + "\n\n";
			sInfo+="Fault Info: " + event.fault.faultString;
			//Alert.show(sInfo, "CONFIG");	
			ComMethods.showErrorTips();
		}
	}
}